
//
//  CharacterLayer.cpp
//  ATM Killer
//
//  Created by USoft on 7/25/14.
//
//

#include "CharacterLayer.h"
CharacterLayer::CharacterLayer() {
    this->tabArray = __Array::create();
    CC_SAFE_RETAIN(this->tabArray);
    this->avatarArray = __Array::create();
    CC_SAFE_RETAIN(this->avatarArray);
    this->isPopUpShow = false;
    this->currentCharacterID = 1;
    this->createLayers();
    this->createLayersContent();
}

CharacterLayer::~CharacterLayer() {
    Utils::safeRelease(tabArray);
}

void CharacterLayer::onEnter() {
    Layer::onEnter();
    touchesRegister();
}

void CharacterLayer::onExit() {
    Layer::onExit();
    this->removeAllChildrenWithCleanup(true);
}

void CharacterLayer::touchesRegister() {
    auto listener = EventListenerTouchAllAtOnce::create();
    listener->onTouchesBegan = CC_CALLBACK_2(CharacterLayer::onTouchesBegan, this);
    listener->onTouchesEnded = CC_CALLBACK_2(CharacterLayer::onTouchesEnded, this);
    _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
}

void CharacterLayer::onTouchesBegan(const std::vector<Touch*>& touches, Event* event)
{
    for(auto touch : touches){
        Point point = this->convertTouchToNodeSpace(touch);
        for(int i=0;i< avatarArray->count();i++)
        {
            Face* avatar = (Face*)avatarArray->getObjectAtIndex(i);
            Face* currentAvatar = (Face*)avatarArray->getObjectAtIndex(this->currentCharacterID);
            RectBody* tempBody = new RectBody(avatar->getContentSize().width,avatar->getContentSize().height);
            tempBody->setPosition(Point(avatar->getPositionX(),avatar->getPositionY()));
            if (Utils::checkPointInRect(tempBody,point) && this->currentCharacterID != i){
                currentAvatar->setSpriteFrame(ANI_BUTTON_AVATAR_2);
                avatar->setSpriteFrame(ANI_BUTTON_AVATAR_1);
                currentAvatar->setLocalZOrder(9-this->currentCharacterID);
                avatar->setLocalZOrder(11);
                this->currentCharacterID = i;
                for(int j=0; j<this->tabArray->count();j++)
                {
                    Face* tabUpgrade = (Face*) this->tabArray->getObjectAtIndex(j);
                    tabUpgrade->removeFromParentAndCleanup(true);
                }
                this->tabArray->removeAllObjects();
                this->createUpgradeTabs();
            }
        }
    }
}

void CharacterLayer::onTouchesEnded(const std::vector<Touch*>& touches, Event* event)
{
    
}

void CharacterLayer::createLayers() {
    this->grayLayer = UGLayerColor::create();
    this->grayLayer->initWithColor(Color4B(0,0,0,100));
    this->addChild(this->grayLayer);
    this->grayLayer->setVisible(false);
    
    this->mainLayer = UGLayer::create();
    this->addChild(this->mainLayer);
    
    this->upgradeLayer = UGLayer::create();
    
    this->popUpLayer = UGLayer::create();
    this->addChild(this->popUpLayer);
}

void CharacterLayer::createLayersContent() {
    GameModel* gameModel = GameModel::getInstance();
    Size gameSize = gameModel->getDisplayResolutionSize();
    //load character board
    this->loadCharacterBoard();
    //load popUp Face;
    this->popUpFace = new Face();
    this->popUpFace->initWithSpriteFrameName(ANI_POP_UP);
    this->popUpFace->setPosition(gameSize.width/2, gameSize.height/2);
    this->popUpLayer->addChild(this->popUpFace);
    
    Label* labelPopUp1 = Utils::createLabel((char*)"Are you sure to spend", (char*)FONT_BANGERS,28);
    labelPopUp1->setColor(Color3B(255,255,255));
    labelPopUp1->setPosition(this->popUpFace->getContentSize().width/2, 200);
    labelPopUp1->setTag(1);
    
    Label* labelPopUp2 = Utils::createLabel((char*)"$123.456 Million", (char*)FONT_BANGERS,50);
    labelPopUp2->setColor(Color3B(255,255,0));
    labelPopUp2->setPosition(this->popUpFace->getContentSize().width/2, 150);
    labelPopUp2->setTag(2);
    
    Label* labelPopUp3 = Utils::createLabel((char*)"Kid gain 34 Dps this upgrade level up to 124",17);
    labelPopUp3->setColor(Color3B(255,255,255));
    labelPopUp3->setPosition(this->popUpFace->getContentSize().width/2, 102);
    labelPopUp3->setTag(3);
    
    this->popUpFace->addChild(labelPopUp1);
    this->popUpFace->addChild(labelPopUp2);
    this->popUpFace->addChild(labelPopUp3);
    UGMenu* popUpMenu = UGMenu::create();
    popUpMenu->setPosition(Point::ZERO);
    // OK Button and Cancel button
    this->okButton = Utils::createButton((char*)ANI_POP_UP_BUTTON_1, (char*)ANI_POP_UP_BUTTON_2, (char*)"OK", 35, (char*)FONT_UVNHAILONG, Color3B(255,255,0));
    this->okButton->setPosition(125,50);
    this->cancelButton = Utils::createButton((char*)ANI_POP_UP_BUTTON_1, (char*)ANI_POP_UP_BUTTON_2, (char*)"Cancel", 35, (char*)FONT_UVNHAILONG, Color3B(255,255,0));
    cancelButton->setPosition(275,50);
    popUpMenu->addChild(this->okButton);
    popUpMenu->addChild(this->cancelButton);
    this->cancelButton->setCallback(CC_CALLBACK_1(CharacterLayer::cancelPopUp,this));
    this->popUpFace->addChild(popUpMenu);
    this->popUpFace->setVisible(false);
}

void CharacterLayer::update(float dt) {
    GameModel* gameModel = GameModel::getInstance();
    //update based on GS
    if(gameModel->getGameState()==GS_CHARACTER)
    {
        this->grayLayer->setVisible(true);
        this->mainLayer->setVisible(true);
        this->popUpLayer->setVisible(true);
        this->upgradeLayer->setVisible(true);
    } else {
        this->grayLayer->setVisible(false);
        this->mainLayer->setVisible(false);
        this->popUpLayer->setVisible(false);
        this->upgradeLayer->setVisible(false);

    }
    //update character and status
    __Array* allKeys = gameModel->getCharacters()->allKeys();
    if (allKeys!=NULL) {
        for(int i=0; i < allKeys->count();i++){
            __Integer* key = (__Integer*) allKeys->getObjectAtIndex(i);
            Character* character = (Character*) gameModel->getCharacters()->objectForKey(key->getValue());
            if (gameModel->getGameState() == GS_PLAY || gameModel->getGameState()== GS_CHARACTER) {
                if(character->getLevel()>0) {
                    this->fadeObject(255, character);
                } else {
                    this->fadeObject(0, character);
                }
            }
        }
    }
    //update gray layer and information
    if (gameModel->getGameState() == GS_PLAY) {
        this->grayLayer->setVisible(false);
        this->characterBoard->setVisible(false);
    } else if (gameModel->getGameState() == GS_CHARACTER) {
        this->grayLayer->setVisible(true);
        this->characterBoard->setVisible(true);
        for(int i=0; i<avatarArray->count();i++)
        {
            Face* avatar = (Face*)avatarArray->getObjectAtIndex(i);
            avatar->setVisible(true);
            
        }
        //update current character
        //information tab
        Character* currentCharacter = (Character*)gameModel->getCharacters()->objectForKey(this->currentCharacterID);
        EntityCharacter* entityCharacter = (EntityCharacter*)gameModel->getEttRepository()->getCharacterList()->objectForKey(this->currentCharacterID);
        {
            char informationText[100];
            char* characterDps;
            char* characterMoney = currentCharacter->getCharacterMoney()->display();
            if(currentCharacter->getLevel()>0){
                characterDps = (char*)currentCharacter->getDps()->display();
            }
            else {
                characterDps = new char[5];
                sprintf(characterDps,"0");
            }
            sprintf(informationText,TXT_CHARACTER_INFORMATION,currentCharacter->getName().c_str(),characterDps, characterMoney);
            delete[] characterDps;
            delete[] characterMoney;
            Label *informationLabel = Utils::createLabel(informationText, 17);
            informationLabel->setColor(Color3B(255,255,255));
            informationLabel->setPosition(informationLabel->getContentSize().width/2 + 25,45);
            informationLabel->setHorizontalAlignment(TextHAlignment::LEFT);
            
            this->informationTab->removeAllChildren();
            this->informationTab->addChild(informationLabel);
            //face
            char characterFacePath[200];
            char* normalStatePattern = (char*) "";
            normalStatePattern = currentCharacter->getAnimation();
            sprintf(characterFacePath, normalStatePattern,currentCharacter->getUpgradeLevel()+1);
            this->characterFace->setSpriteFrame(characterFacePath);
            //level-up button
            if(currentCharacter->getLevel()< entityCharacter->getMaxLevel())
            {
                this->levelUpButton->setEnabled(true);
                char levelUpText[100];
                sprintf(levelUpText,TXT_CHARACTER_LEVEL_UP,currentCharacter->getNextLevel());
                Label *normalLabel = (Label*)this->levelUpButton->getNormalImage()->getChildByTag(1);
                normalLabel->setString(levelUpText);
                normalLabel->setDimensions(this->levelUpButton->getContentSize().width, this-> levelUpButton->getContentSize().height);
                Label *selectedLabel = (Label*)this->levelUpButton->getSelectedImage()->getChildByTag(1);
                selectedLabel->setDimensions(this->levelUpButton->getContentSize().width, this-> levelUpButton->getContentSize().height);
                selectedLabel->setString(levelUpText);

            } else {
                this->levelUpButton->setEnabled(false);
            }
        }
        //upgrade column
        for(int i=0; i<this->tabArray->count();i++)
        {
            Face* upgradeTab = (Face*) tabArray->getObjectAtIndex(i);
            EntityCharacterAvatar* characterUpgradeInfo =(EntityCharacterAvatar*) entityCharacter->getAvatars()->getObjectAtIndex(i);
            //earn Label
            Label* upgradeEarnLabel = (Label*)upgradeTab->getChildByTag(0);
            upgradeEarnLabel->setDimensions(300, 100);
            BigNumber* upgradeEarn = (BigNumber*)currentCharacter->getUpgradeLevelEarn()->getObjectAtIndex(i);
            char upgradeEarnText[100];
            char* earnString = upgradeEarn->display();
            sprintf(upgradeEarnText,TXT_MONEY_FORMAT,earnString);
            delete[]earnString;
            upgradeEarnLabel->setString(upgradeEarnText);
            //required level
            char upgradeRequiredText[100];
            sprintf(upgradeRequiredText,TXT_CHARACTER_UPGRADE_REQUIRE,characterUpgradeInfo->getRequiredLevel());
            Label* upgradeRequiredLabel = (Label*)upgradeTab->getChildByTag(3);
            upgradeRequiredLabel->setString(upgradeRequiredText);
            //upgradeCharacterName
            char upgradeNameText[100];
            sprintf(upgradeNameText,"%s",characterUpgradeInfo->getCaption().c_str());
            Label* upgradeNameLabel = (Label*)upgradeTab->getChildByTag(2);
            upgradeNameLabel->setString(upgradeNameText);
            upgradeNameLabel->setDimensions(upgradeNameLabel->getContentSize().width,upgradeNameLabel->getContentSize().height);
            //effect
            char upgradeEffectText[100];
            sprintf(upgradeEffectText,TXT_CHARACTER_UPGRADE_EFFECT,currentCharacter->getName().c_str(),characterUpgradeInfo->getEffect().c_str());
            Label *upgradeEffectLabel = (Label*)upgradeTab->getChildByTag(1);
            upgradeEffectLabel->setString(upgradeEffectText);
            
            if(currentCharacter->getUpgradeLevel() > i-1){
                upgradeTab->getChildByTag(0)->setVisible(true);
                upgradeTab->getChildByTag(1)->setVisible(true);
                upgradeTab->getChildByTag(2)->setVisible(true);
                upgradeTab->getChildByTag(3)->setVisible(false);
                upgradeTab->getChildByTag(4)->setVisible(false);
            }
            else if(currentCharacter->getUpgradeLevel() == i-1 && characterUpgradeInfo->getRequiredLevel()<= currentCharacter->getLevel()){
                upgradeTab->getChildByTag(0)->setVisible(false);
                upgradeTab->getChildByTag(1)->setVisible(true);
                upgradeTab->getChildByTag(2)->setVisible(true);
                upgradeTab->getChildByTag(3)->setVisible(false);
                upgradeTab->getChildByTag(4)->setVisible(true);
            }
            else if (currentCharacter->getUpgradeLevel() < i-1 || characterUpgradeInfo->getRequiredLevel() > currentCharacter->getLevel()){
                upgradeTab->getChildByTag(0)->setVisible(false);
                upgradeTab->getChildByTag(1)->setVisible(false);
                upgradeTab->getChildByTag(2)->setVisible(false);
                upgradeTab->getChildByTag(3)->setVisible(true);
                upgradeTab->getChildByTag(4)->setVisible(false);
            }
        }
        
    }
    //disabled button when Pop up show
//    if(this->isPopUpShow == true){
//        this->levelUpButton->setEnabled(false);
//    }else {
//        this->levelUpButton->setEnabled(true);
//    }
    
}
void CharacterLayer::loadCharacterBoard()
{
    GameModel* gameModel = GameModel::getInstance();
    Size gameSize = gameModel->getDisplayResolutionSize();
    Character* currentCharacter = (Character*)gameModel->getCharacters()->objectForKey(this->currentCharacterID);
    EntityCharacter* entityCharacter = (EntityCharacter*)gameModel->getEttRepository()->getCharacterList()->objectForKey(this->currentCharacterID);
    
    //create characterBoard
    this->characterBoard = new Face();
    this->characterBoard->initWithSpriteFrameName(ANI_BOARD_CHARACTER);
    this->characterBoard->setPosition(gameSize.width/2,gameSize.height/2);
    this->characterBoard->setVisible(false);
    this->mainLayer->addChild(this->characterBoard,10);
    
    Label* characterTitle = Utils::createLabel((char*)"Characters Upgrade", (char*)FONT_UVNHAILONG, 45);
    characterTitle->setPosition(this->characterBoard->getContentSize().width/2,this->characterBoard->getContentSize().height - 30);
    this->characterBoard->addChild(characterTitle);
    //create Tabs in characterBoard
    //create character avatars
    for(int i=0;i<gameModel->getCharacters()->count();i++)
    {
        Face* avatar = new Face();
        if(i == this->currentCharacterID){
            avatar->initWithSpriteFrameName(ANI_BUTTON_AVATAR_1);
            this->mainLayer->addChild(avatar,11);
        } else{
            avatar->initWithSpriteFrameName(ANI_BUTTON_AVATAR_2);
            this->mainLayer->addChild(avatar,gameModel->getCharacters()->count()-i);
        }
        avatar->setPosition(0+this->characterBoard->getPositionX()-this->characterBoard->getContentSize().width/2,this->characterBoard->getPositionY()-this->characterBoard->getContentSize().height/2+this->characterBoard->getContentSize().height - 47 - i*(avatar->getContentSize().height-22));
        
        avatar->setVisible(false);
        this->avatarArray->addObject(avatar);
    }
    //informationTab
    this->informationTab = new Face();
    this->informationTab->initWithSpriteFrameName(ANI_TAB_INFO_CHARACTER);
    this->informationTab->setPosition(INFORMATION_TAB_X,INFORMATION_TAB_Y);
    this->characterBoard->addChild(this->informationTab);
    
    //create level up button
    char levelUpText[100];
    sprintf(levelUpText,TXT_CHARACTER_LEVEL_UP,currentCharacter->getNextLevel());
    this->levelUpButton = Utils::createButton((char*)ANI_BUTTON_LEVEL_UP_1,(char*)ANI_BUTTON_LEVEL_UP_2, levelUpText, 35, (char*)FONT_UVNHAILONG, Color3B(255,255,0));
        //create disabled image
    {
        Sprite* disabledSprite = Sprite::createWithSpriteFrameName((char*)ANI_BUTTON_LEVEL_UP_1);
        Label* disabledText = Utils::createLabel(TXT_MAX_LEVEL,(char*) FONT_UVNHAILONG, 35);
        disabledText->setColor(Color3B(255,255,0));
        disabledText->setDimensions(disabledText->getContentSize().width+20,0);
        disabledText->setPosition(Point(this->levelUpButton->getContentSize().width / 2, this->levelUpButton->getContentSize().height / 2+3));
        disabledText->enableShadow();
        disabledSprite->addChild(disabledText);
        this->levelUpButton->setDisabledImage(disabledSprite);
    }
    
    UGMenu* characterMenu = UGMenu::create();
    characterMenu->setPosition(Point::ZERO);
    this->levelUpButton->setPosition(220,325);
    characterMenu->addChild(this->levelUpButton);
    this->characterBoard->addChild(characterMenu);
    this->levelUpButton->setTag(currentCharacter->getCharacterId());
    this->levelUpButton->setCallback(CC_CALLBACK_1(CharacterLayer::showLevelUpPopUp,this));
    //create upgrade collumn
    int heightUpgradeLayer = (int)entityCharacter->getAvatars()->count() * 110-23;
    int widthUpgradeLayer = this->characterBoard->getContentSize().width/2+10;
    this->createUpgradeTabs();
    this->upgradeLayer->setPosition(0,0);
    this->upgradeLayer->setContentSize(Size(widthUpgradeLayer,heightUpgradeLayer));
    this->upgradeContentView = ScrollView::create(Size(this->characterBoard->getContentSize().width/2, this->characterBoard->getContentSize().height- 106));
    this->upgradeContentView->setPosition(400,40);
    this->upgradeContentView->setContainer(this->upgradeLayer);
    this->upgradeContentView->setDirection(ScrollView::Direction::VERTICAL);
    this->upgradeContentView->setContentOffset(Point(0,this->characterBoard->getContentSize().height-entityCharacter->getAvatars()->count()*100-152));
    this->characterBoard->addChild(this->upgradeContentView);
    
    //get current character Information
        //information tabs
    char informationText[100];
    char* characterDps;
    char* characterMoney = currentCharacter->getCharacterMoney()->display();
    if(currentCharacter->getLevel()>0){
        characterDps = (char*)currentCharacter->getDps()->display();
    }
    else {
        characterDps = new char[5];
        sprintf(characterDps,"0");
    }
    sprintf(informationText,TXT_CHARACTER_INFORMATION,currentCharacter->getName().c_str(),characterDps, characterMoney);
    delete[] characterDps;
    delete[] characterMoney;
    Label *informationLabel = Utils::createLabel(informationText, 17);
    informationLabel->setColor(Color3B(255,255,255));
    informationLabel->setPosition(informationLabel->getContentSize().width/2 + 25,45);
    informationLabel->setHorizontalAlignment(TextHAlignment::LEFT);
    this->informationTab->addChild(informationLabel);
        //face
    this->characterFace = new Face();
    this->characterFace->init();
    char characterFacePath[200];
    char* normalStatePattern = (char*) "";
    normalStatePattern = currentCharacter->getAnimation();
    sprintf(characterFacePath, normalStatePattern,currentCharacter->getUpgradeLevel()+1);
    this->characterFace->initWithSpriteFrameName(characterFacePath);
    this->characterBoard->addChild(this->characterFace);
    this->characterFace->setPosition(175,200);
}

void CharacterLayer::createUpgradeTabs(){
    GameModel* gameModel = GameModel::getInstance();
    Character* currentCharacter = (Character*)gameModel->getCharacters()->objectForKey(this->currentCharacterID);
    EntityCharacter* entityCharacter = (EntityCharacter*)gameModel->getEttRepository()->getCharacterList()->objectForKey(this->currentCharacterID);
    for(int i=0;i<entityCharacter->getAvatars()->count();i++)
    {
        EntityCharacterAvatar* characterUpgradeInfo = (EntityCharacterAvatar*)entityCharacter->getAvatars()->getObjectAtIndex(i);
        Face* upgradeTab = new Face();
        upgradeTab->initWithSpriteFrameName(ANI_BUTTON_PAUSE_MENU);
        upgradeTab->setPosition(upgradeTab->getContentSize().width/2-5, 10+entityCharacter->getAvatars()->count()*upgradeTab->getContentSize().height - i*(upgradeTab->getContentSize().height +10));
        //unlock button
        MenuItemSprite* unlockButton = Utils::createButton((char*)ANI_POP_UP_BUTTON_1,(char*)ANI_POP_UP_BUTTON_2,(char*)TXT_CHARACTER_UPGRADE_UNLOCK,35, (char*)FONT_UVNHAILONG, Color3B(255,255,0));
        unlockButton->setPosition(365,45);
        UGMenu* upgradeTabMenu = UGMenu::create();
        upgradeTabMenu->setPosition(Point::ZERO);
        upgradeTabMenu->addChild(unlockButton);
        upgradeTabMenu->setTag(4); //tag 4
        upgradeTab->addChild(upgradeTabMenu);
        unlockButton->setCallback(CC_CALLBACK_1(CharacterLayer::showUpgradePopUp, this));
        //required level
        char upgradeRequiredText[100];
        sprintf(upgradeRequiredText,TXT_CHARACTER_UPGRADE_REQUIRE,characterUpgradeInfo->getRequiredLevel());
        Label* upgradeRequiredLabel = Utils::createLabel(upgradeRequiredText,(char*)FONT_UVNHAILONG,45);
        upgradeRequiredLabel->setPosition(upgradeTab->getContentSize().width/2,upgradeTab->getContentSize().height/2);
        upgradeRequiredLabel->setTag(3); //tag 3
        upgradeTab->addChild(upgradeRequiredLabel);
        
        //upgradeCharacterName
        char upgradeNameText[100];
        sprintf(upgradeNameText,"%s",characterUpgradeInfo->getCaption().c_str());
        Label* upgradeNameLabel = Utils::createLabel(upgradeNameText,(char*)FONT_UVNHAILONG,35);
        upgradeNameLabel->setAnchorPoint(Point(0,0));
        upgradeNameLabel->setPosition(23,43);
        upgradeNameLabel->setTag(2); // tag 2
        upgradeTab->addChild(upgradeNameLabel);
        
        //effect
        char upgradeEffectText[100];
        sprintf(upgradeEffectText,TXT_CHARACTER_UPGRADE_EFFECT,currentCharacter->getName().c_str(),characterUpgradeInfo->getEffect().c_str());
        Label *upgradeEffectLabel = Utils::createLabel(upgradeEffectText, 17);
        upgradeEffectLabel->setHorizontalAlignment(TextHAlignment::LEFT);
        upgradeEffectLabel->setAnchorPoint(Point(0,0));
        upgradeEffectLabel->setPosition(30,25);
        upgradeEffectLabel->setTag(1);// tag 1
        upgradeTab->addChild(upgradeEffectLabel);
        //upgradeEarn
        BigNumber* upgradeEarn = (BigNumber*)currentCharacter->getUpgradeLevelEarn()->getObjectAtIndex(i);
        char upgradeEarnText[100];
        char* earnString = upgradeEarn->display();
        sprintf(upgradeEarnText,TXT_MONEY_FORMAT,earnString);
        delete[]earnString;
        Label* upgradeEarnLabel = Utils::createLabel(upgradeEarnText,(char*)FONT_UVNHAILONG, 30);
        upgradeEarnLabel->setPosition(330,35);
        upgradeTab->addChild(upgradeEarnLabel);
        upgradeEarnLabel->setTag(0); // tag 0
        
        this->upgradeLayer->addChild(upgradeTab);
        this->tabArray->addObject(upgradeTab);
    }

}
void CharacterLayer::showLevelUpPopUp(Ref* sender)
{
    GameModel* gameModel = GameModel::getInstance();
    Size gameSize = gameModel->getDisplayResolutionSize();
    Character* currentCharacter = (Character*)gameModel->getCharacters()->objectForKey(this->currentCharacterID);
    EntityCharacter* entityCharacter = (EntityCharacter*)gameModel->getEttRepository()->getCharacterList()->objectForKey(this->currentCharacterID);
    if(gameModel->getScore()->compare(currentCharacter->getCost())>=0){
        // set label
        char labelPopUp1Text[200];
        sprintf(labelPopUp1Text, TXT_SURE_LEVEL_UP);
        Label* labelPopUp1 = (Label*) this->popUpFace->getChildByTag(1);
        labelPopUp1->setString(labelPopUp1Text);
        labelPopUp1->setPosition(this->popUpFace->getContentSize().width/2, 200);
        
        char labelPopUp2Text[200];
        char* costCharacter = currentCharacter->getCost()->display();
        sprintf(labelPopUp2Text,TXT_MONEY_FORMAT, costCharacter);
        delete[] costCharacter;
        Label* labelPopUp2 = (Label*)this->popUpFace->getChildByTag(2);
        labelPopUp2->setPosition(this->popUpFace->getContentSize().width/2, 150);
        labelPopUp2->setString(labelPopUp2Text);
        
        char labelPopUp3Text[200];
        char* dpsCharacter = entityCharacter->getDps()->toString();
        sprintf(labelPopUp3Text,TXT_HINT_LEVEL_UP,currentCharacter->getName().c_str(),dpsCharacter,currentCharacter->getNextLevel());
        delete[] dpsCharacter;
        Label* labelPopUp3 = (Label*)this->popUpFace->getChildByTag(3);
        labelPopUp3->setString(labelPopUp3Text);
        labelPopUp3->setVisible(true);
        
        this->getCancelButton()->setVisible(true);
        this->getOkButton()->setPosition(125,50);
        
    } else {
        char labelPopUp1Text[200];
        sprintf(labelPopUp1Text, TXT_NOT_ENOUGH_MONEY);
        Label* labelPopUp1 = (Label*) this->popUpFace->getChildByTag(1);
        labelPopUp1->setString(labelPopUp1Text);
        labelPopUp1->setPosition(this->popUpFace->getContentSize().width/2, 180);
        
        char labelPopUp2Text[200];
        char* costCharacter = currentCharacter->getCost()->display();
        sprintf(labelPopUp2Text,TXT_MONEY_FORMAT, costCharacter);
        delete[] costCharacter;
        Label* labelPopUp2 = (Label*)this->popUpFace->getChildByTag(2);
        labelPopUp2->setString(labelPopUp2Text);
        labelPopUp2->setPosition(this->popUpFace->getContentSize().width/2, 110);
        
        Label* labelPopUp3 = (Label*)this->popUpFace->getChildByTag(3);
        labelPopUp3->setVisible(false);
        this->getOkButton()->setPosition(200,50);
        this->getCancelButton()->setVisible(false);
    }
    // set callback
    this->getOkButton()->setCallback(CC_CALLBACK_1(CharacterLayer::onButtonClick,this));
    this->getOkButton()->setTag(1);
    
    this->popUpFace->setVisible(true);
    this->isPopUpShow = true;
}

void CharacterLayer::showUpgradePopUp(Ref* sender){
    GameModel* gameModel = GameModel::getInstance();
    Size gameSize = gameModel->getDisplayResolutionSize();
    Character* currentCharacter = (Character*)gameModel->getCharacters()->objectForKey(this->currentCharacterID);
    EntityCharacter* entityCharacter = (EntityCharacter*)gameModel->getEttRepository()->getCharacterList()->objectForKey(this->currentCharacterID);
    EntityCharacterAvatar* upgradeCharacterInfo = (EntityCharacterAvatar*) entityCharacter->getAvatars()->getObjectAtIndex(currentCharacter->getUpgradeLevel()+1);
    if(gameModel->getScore()->compare(upgradeCharacterInfo->getPrice())>=0)
    {
        // set label
        char labelPopUp1Text[200];
        sprintf(labelPopUp1Text, TXT_SURE_LEVEL_UP);
        Label* labelPopUp1 = (Label*) this->popUpFace->getChildByTag(1);
        labelPopUp1->setString(labelPopUp1Text);
        labelPopUp1->setPosition(this->popUpFace->getContentSize().width/2, 200);

        char labelPopUp2Text[200];
        char* costUpgrade = upgradeCharacterInfo->getPrice()->display();
        sprintf(labelPopUp2Text,TXT_MONEY_FORMAT, costUpgrade);
        delete[]costUpgrade;
        Label* labelPopUp2 = (Label*)this->popUpFace->getChildByTag(2);
        labelPopUp2->setPosition(this->popUpFace->getContentSize().width/2, 150);
        labelPopUp2->setString(labelPopUp2Text);
        
        char labelPopUp3Text[200];
        string effect = upgradeCharacterInfo->getEffect();
        sprintf(labelPopUp3Text,TXT_HINT_UPGRADE,currentCharacter->getName().c_str(),effect.c_str(),currentCharacter->getUpgradeLevel()+1);
        Label* labelPopUp3 = (Label*)this->popUpFace->getChildByTag(3);
        labelPopUp3->setString(labelPopUp3Text);
        labelPopUp3->setVisible(true);
        
        this->getCancelButton()->setVisible(true);
        this->getOkButton()->setPosition(125,50);
    } else {
        char labelPopUp1Text[200];
        sprintf(labelPopUp1Text, TXT_NOT_ENOUGH_MONEY);
        Label* labelPopUp1 = (Label*) this->popUpFace->getChildByTag(1);
        labelPopUp1->setString(labelPopUp1Text);
        labelPopUp1->setPosition(this->popUpFace->getContentSize().width/2, 180);
        
        char labelPopUp2Text[200];
        char* costCharacter = upgradeCharacterInfo->getPrice()->display();
        sprintf(labelPopUp2Text,TXT_MONEY_FORMAT, costCharacter);
        delete[] costCharacter;
        Label* labelPopUp2 = (Label*)this->popUpFace->getChildByTag(2);
        labelPopUp2->setString(labelPopUp2Text);
        labelPopUp2->setPosition(this->popUpFace->getContentSize().width/2, 110);
        
        Label* labelPopUp3 = (Label*)this->popUpFace->getChildByTag(3);
        labelPopUp3->setVisible(false);
        
        this->getOkButton()->setPosition(200,50);
        this->getCancelButton()->setVisible(false);

    }
        // set callback
        this->getOkButton()->setCallback(CC_CALLBACK_1(CharacterLayer::onButtonClick,this));
        this->getOkButton()->setTag(2);
        
        this->popUpFace->setVisible(true);
        this->isPopUpShow = true;
    
}

void CharacterLayer::cancelPopUp(Ref* sender){
    this->isPopUpShow = false;
    this->popUpFace->setVisible(false);
}

void CharacterLayer::onButtonClick(Ref* sender){
    //level up
    {
        if (this->getOkButton()->getTag() == 1) {
            int characterId = this->currentCharacterID;
            this->notify(REQ_UPGRADE_CHARACTER, EVT_CLICK_BUTTON, NULL, 0, __Integer::create(characterId));
            this->cancelPopUp(sender);
        }
    //special upgrade
        if (this->getOkButton()->getTag() == 2) {
            int characterId = this->currentCharacterID;
            this->notify(REQ_SPECIAL_UPGRADE_CHARACTER, EVT_CLICK_BUTTON, NULL, 0, __Integer::create(characterId));
            this->cancelPopUp(sender);
        }
    }
}


void CharacterLayer::fadeObject(int fade, Obj* obj){
    if (obj == NULL) {
        return;
    }
    for (int i = 0; i < obj->getFaces()->count(); i++) {
        Face* currentFace = (Face*)obj->getFaces()->getObjectAtIndex(i);
        currentFace->setPosition(obj->getBody()->getPosition());
        currentFace->setOpacity(fade);
    }
}